<?php
/**
 * @brief Page 分页处理类
 * @class Page
 * @note
 */
namespace  Scshux\Utils;
use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
class Page
{
	private $fields;

	/**
	 * 数据库操作对象
	 * @var \Phalcon\Db\Adapter\Pdo\Mysql
	 */
	private $db;

	private $sql;
	private $rows;
	public $index;//当前页数
	public $totalpage;//总页数
	public $pagesize;//每页的条数
	public $firstpage;//第一页
	public $lastpage;//最后一页
	public $pagelength;//要展示的页面数


    /**
     * @brief 构造函数
     * @param string $sql 要分页的SQL语句
     * @param int $pagesize 每页的记录
     * @param int $pagelength 展示pageBar的页数
     */
	public function __construct($sql="",$pagesize=20,$pagelength=10)
	{
		$this->pagesize=$pagesize;
		$this->pagelength=$pagelength;

		$this->db =Di::getDefault()->get('db');

		if($sql!="")
		{
			$this->setSql($sql);
		}
	}
    /**
     * @brief 分析要分页的SQl语句
     * @param string $sql SQL语句
     */
	public function setSql($sql)
	{
		$this->sql=$sql;
        if(strpos($sql,'GROUP BY') === false)
        {
	        $endstr = strstr($this->sql,'from');
	        $endstr = preg_replace('/^(.*)order\s+by.+$/i','$1',$endstr);
	        $countSql = "select count(*) as total ".$endstr;

	        $result = $this->db->query($countSql);
	        $result->setFetchMode(\PDO::FETCH_OBJ);
	        $countRow = $result->fetch();

        }
        else
        {
        	$countSql = "select count(*) as total from (".$sql.") as IPaging";

        	$result = $this->db->query($countSql);
        	$result->setFetchMode(\PDO::FETCH_OBJ);
        	$countRow = $result->fetch();
        }

		$this->rows = intval($countRow->total);

		$this->firstpage=1;
		$this->totalpage=floor(($this->rows-1)/$this->pagesize)+1;

		$this->lastpage=$this->firstpage+$this->totalpage-1;
		if($this->lastpage>$this->totalpage)$this->lastpage=$this->totalpage;

	}

	/**
	 * 获取总条数
	 * @return number
	 */
	public  function  getTotalSize()
	{
		return $this->rows;
	}

    /**
     * @brief 得到对应要查询分页的数据内容
     * @param int  $page要查询的页数
     * @return Array 数据
     */
	public function getPage($page)
	{
		$page=intval($page);
		$this->index=$page;
		if($page<=0)$this->index=1;
		if($this->totalpage>0)
		{
			if($page>$this->totalpage)$this->index=$this->totalpage;
			$this->firstpage=$this->index-floor($this->pagelength/2);
			if($this->firstpage<=0)$this->firstpage=1;
			$this->lastpage=$this->firstpage+$this->pagelength-1;
			if($this->lastpage>$this->totalpage)
			{
				$this->lastpage=$this->totalpage;
				$this->firstpage=($this->totalpage-$this->pagelength+1)>1?$this->totalpage-$this->pagelength+1:1;
			}

			$result = $this->db->query($this->sql." limit ".($this->index-1)*$this->pagesize.",".($this->pagesize));
			$result->setFetchMode(\PDO::FETCH_OBJ);
			return  $result->fetchAll();
		}
		else return array();
	}

	/**
	 * 获取分页
	 * @param   $type   分页类型   1-2-3   pc端     4  手机端
	 */
	public  function getPager($type=1)
	{
		$url = $_GET['_url'];
		unset($_GET['_url']);
		unset($_GET['_page']);
		$lastUrl = FactoryDefault::getDefault()->get('url')->get($url,$_GET);
		if(empty($_GET)){
			$lastUrl .= '?_page=$$';
		}else{
			$lastUrl .= '&_page=$$';
		}
		$params = array(
				'total_rows'=> $this->getTotalSize(), #(必须)
				'method'    =>'html', #(必须)
				'parameter' =>$lastUrl,  #(必须)
				'now_page'  => $this->getIndex(),  #(必须)
				'list_rows' => $this->getPageSize(), #(可选) 默认为15
		);
		$pagerObj = new Pager($params);
		return  $pagerObj->show($type);
	}

	/**
	 * pc  mobile  分页数据一起返回
	 */
	public  function getPagers()
	{
		$url = $_GET['_url'];
		unset($_GET['_url']);
		unset($_GET['_page']);
		$lastUrl = FactoryDefault::getDefault()->get('url')->get($url,$_GET);
		if(empty($_GET)){
			$lastUrl .= '?_page=$$';
		}else{
			$lastUrl .= '&_page=$$';
		}
		$params = array(
				'total_rows'=> $this->getTotalSize(), #(必须)
				'method'    =>'html', #(必须)
				'parameter' =>$lastUrl,  #(必须)
				'now_page'  => $this->getIndex(),  #(必须)
				'list_rows' => $this->getPageSize(), #(可选) 默认为15
		);
		$pagerObj = new Pager($params);
		return  $pagerObj->shows();
	}
	
    /**
     * @brief 获取当前分页数
	 * @return int 分页数
	 */
	public function getIndex()
	{
		return $this->index;
	}
    /**
     * @brief 获取分页总数
	 * @return int 分页总数
	 */
	public function getTotalPage()
	{
		return $this->totalpage;
	}
    /**
     * @brief 设置展示的分页数量
	 * @return int 分页数量
	 */
	public function setPageLength($legth)
	{
		$this->pagelength=$legth;
	}
    /**
     * @brief 获取展示的分页数量
	 * @return int 分页长度
	 */
	public function getPageLength()
	{
		return $this->pagelength;
	}
    /**
     * @brief 设置每页的数据条数
     * @return int 数据条数
	 */
	public function setPageSize($size)
	{
		$this->pagesize  = $size;
		$this->totalpage = floor(($this->rows-1)/$this->pagesize)+1;
	}
    /**
     * @brief 得到单页要展示的数据条数
     * @return int 数据条数
     */
	public function getPageSize()
	{
		return $this->pagesize;
	}
    /**
     * @brief 当前pageBar的第一页
     * @return int 当前pageBar的第一页
     */
	public function getFirstPage()
	{
		return $this->firstpage;
	}
    /**
     * @brief 当前pageBar最得最后一页的页数
     * @return int 当前pageBar最后一页的页数
     */
	public function getLastPage()
	{
		return $this->lastpage;
	}

}
?>
